package com.jt.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jt.pojo.User;
import com.jt.service.UserService;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisCluster;

import java.util.List;

/**
 * @Author: Li Yu
 * @Date: 2020/11/17 15:27
 */
@RestController
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JedisCluster jedisCluster;

    @RequestMapping("findAll")
    public List<User> doFindAll(){
         List<User> userList=userService.doFindUser();
         return userList;
    }

    /**
     * 需求：实现用户信息校验
     * 校验步骤： 接收用户请求、之后利用RestFul获取数据、实现数据库校验，按照jsnp的方式实现跨域
     * url：
     * 参数：  restful方式获取
     * 返回值：JSONPObject
     */
    @RequestMapping("check/{param}/{type}")
    public JSONPObject checkUser(@PathVariable String param, @PathVariable Integer type,String callback){

        //只需要校验数据库中是否有结果
        boolean flag=userService.checkUSer(param,type);
        SysResult sysResult=SysResult.success(flag);
        JSONPObject jsonpObject=new JSONPObject(callback, sysResult);
        return jsonpObject;
    }
    /**
     * http://sso.jt.com/user/testHttpClient
     * 返回List<User>
     */
    @RequestMapping("testHttpClient")
    public List<User> testHttpClient(){

        return userService.doFindUser();
    }

    /**
     * 业务说明：通过跨域方式，获取用户的json数据
     * url：sso.jt.com/user/query
     * 请求参数：ticket信息
     * 返回值：SysResult对象(UserJSON)
     * 需求：通过ticket获取user JSON
     */
    @RequestMapping("query/{ticket}")
    public JSONPObject findUserByTicket(@PathVariable String ticket,String callback){
        String userJSON=jedisCluster.get(ticket);
        if (StringUtils.isEmpty(userJSON)){
            return new JSONPObject(callback, SysResult.fail());
        }else {
            return new JSONPObject(callback, SysResult.success(userJSON));
        }
    }

}
